package com.generator.sysManage.positionManage.service.impl;

import com.generator.aboutUser.module.service.IModuleService;
import com.generator.sysManage.moduleManage.pojo.Module;
import com.generator.sysManage.moduleManage.service.IModuleManageService;
import com.generator.sysManage.positionManage.dao.IJMMap;
import com.generator.sysManage.positionManage.pojo.JMJob;
import com.generator.sysManage.positionManage.service.IJMService;
import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.sysManage.sectionManage.service.IDMService;
import com.generator.sysManage.userManage.pojo.*;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * JMService
 * @author WJW
 * @Date .....
 * @version ......
 */
@Service
public class JMService implements IJMService {

    @Autowired
    private IJMMap ijmMap;

    @Autowired    //获取部门信息
    private IDMService idmService;

    @Autowired   //获取模块信息，方便赋权
    private IModuleManageService iModuleManageService;

    public void setIjmMap(IJMMap ijmMap) {
        this.ijmMap = ijmMap;
    }
    public IJMMap getIjmMap() {
        return ijmMap;
    }

    public IDMService getIdmService() {
        return idmService;
    }
    public void setIdmService(IDMService idmService) {
        this.idmService = idmService;
    }

    public IModuleManageService getiModuleManageService() {
        return iModuleManageService;
    }
    public void setiModuleManageService(IModuleManageService iModuleManageService) {
        this.iModuleManageService = iModuleManageService;
    }

    /**
     * 根据条件查询所有职位集合
     * @param jmJob 查询条件
     * @return 职位集合
     */
    public List<JMJob> getJMJobListByJMJob(JMJob jmJob){
        return ijmMap.getJMJobListByJMJob(jmJob);
    }


    /**
     * 根据条件查询所有职位集合进行分页
     * @param jmJob 查询条件
     * @return pageModel
     */
    public PageModel<JMJob> getJMJobListByFY(JMJob jmJob, PageModel<JMJob> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<JMJob> userList = ijmMap.getJMJobListByJMJob(jmJob);


        PageInfo<JMJob> pageInfo = new PageInfo<JMJob>(userList);

        pageModel.setData(new ArrayList<JMJob>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }


    /**
     * 根据条件查询所有部门
     * @param dmDept 查询条件
     * @return 部门集合
     */
    public List<DMDept> getDeptList(DMDept dmDept){
        return idmService.getDMDeptListByDMDept(dmDept);
    }


    /**
     * 搜索职位集合进行分页
     * @param jmJob 搜索条件
     * @return pageModel
     */
    public PageModel<JMJob> getJMJobListByFY1(JMJob jmJob, PageModel<JMJob> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<JMJob> userList = ijmMap.getJMJobListByJMJob(jmJob);


        PageInfo<JMJob> pageInfo = new PageInfo<JMJob>(userList);

        pageModel.setData(new ArrayList<JMJob>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }


    /**
     * 添加职位
     * @param jmJob 添加的职位信息
     * @return 执行条数
     */
    public Integer addJMJob(JMJob jmJob){
        return ijmMap.addJMJob(jmJob);
    }


    /**
     *进入修改页面
     * @param jmJob 存放job_id
     * @return 页面路径
     */
    public String updateView(JMJob jmJob, Model model, HttpSession session){
        session.setAttribute("job_id",jmJob.getJob_id());
        List<JMJob> dmDeptList = ijmMap.getJMJobListByJMJob(jmJob);
        model.addAttribute("updatePosition",dmDeptList.get(0));
        List<DMDept> cityList = idmService.getDMDeptListByDMDept(new DMDept());
        model.addAttribute("deptList1",cityList);
        return "sys/position/positionUpdate";
    }



    /**
     *修改职位
     * @param jmJob 修改的职位信息
     * @return 执行条数
     */
    public Integer updateJMJob(JMJob jmJob){
        return ijmMap.updateJMJob(jmJob);
    }



    /**
     *修改职位状态
     * @param jmJob 修改的职位信息
     * @return 路径
     */
    public String  updateState(JMJob jmJob, Model model){
        String job_id1 = jmJob.getJob_id();
        String[] split = job_id1.split(",");

        String job_id=split[0];
        JMJob job=new JMJob();
        job.setJob_id(job_id);

        int pageNum=Integer.parseInt(split[1]);
        PageModel<JMJob> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);

        if(split[2].equals("0")){
            job.setJob_state("1");
        }else {
            job.setJob_state("0");
        }

        Integer updateState = ijmMap.updateState(job);
        if(updateState==1){
            JMJob job1=new JMJob();
            PageModel<JMJob> deptList = getJMJobListByFY(job1,pageModel);
            model.addAttribute("positionList",deptList);
            return "sys/position/positionList";
        }
        return "forward:/positionManage/positionList";
    }


    /**
     *搜索后修改职位状态
     * @param jmJob 修改的职位信息
     * @return 搜索后路径
     */
    public String  updateState1(JMJob jmJob, Model model,JMJob jmJob1){
        String job_id1 = jmJob.getJob_id();
        String[] split = job_id1.split(",");

        String job_id=split[0];
        JMJob job=new JMJob();
        job.setJob_id(job_id);

        int pageNum=Integer.parseInt(split[1]);
        PageModel<JMJob> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);

        if(split[2].equals("0")){
            job.setJob_state("1");
        }else {
            job.setJob_state("0");
        }

        Integer updateState = ijmMap.updateState(job);
        if(updateState==1){
            PageModel<JMJob> deptList = getJMJobListByFY(jmJob1,pageModel);
            model.addAttribute("positionList",deptList);
            return "sys/position/positionList1";
        }
        return "forward:/positionManage/positionList1";
    }



    /**
     *进入赋权页面
     * @param permission 存放comrid
     * @return 页面路径
     */
    public String positionGrantView(Permission permission, Model model, HttpSession session){

        //分割获取职位id和当前页数
        String comrid1 = permission.getComrid();
        String[] split = comrid1.split(",");
        String comrid=split[0];
        Permission permission1=new Permission();
        permission1.setComrid(comrid);

        session.setAttribute("comrid",comrid);
        session.setAttribute("grandPageNum",split[1]);


        //获取所有的父模块
        Module module=new Module();
        module.setModule_pid("0");
        List<Module> moduleList = iModuleManageService.getModuleListByJMJob(module);
        model.addAttribute("moduleList",moduleList);

        //获取所有父模块已经对应的子模块
        HashMap<Module,List<Module>> moduleHashMap=new HashMap<>();
        Module module2=new Module();
        for (Module module1 : moduleList) {
            module2.setModule_pid(module1.getModule_id());
            List<Module> moduleListByJMJob = iModuleManageService.getModuleListByJMJob(module2);
            moduleHashMap.put(module1,moduleListByJMJob);
        }
        model.addAttribute("moduleLists",moduleHashMap);

        return "sys/position/positionGrant";
    }


    /**
     * 搜索后的进入赋权页面
     * @param permission 存放comrid
     * @return 页面路径
     */
    public String positionGrantView1(Permission permission, Model model, HttpSession session){

        //分割获取职位id和当前页数
        String comrid1 = permission.getComrid();
        String[] split = comrid1.split(",");
        String comrid=split[0];
        Permission permission1=new Permission();
        permission1.setComrid(comrid);

        session.setAttribute("comrid",comrid);
        session.setAttribute("grandPageNum",split[1]);


        //获取所有的父模块
        Module module=new Module();
        module.setModule_pid("0");
        List<Module> moduleList = iModuleManageService.getModuleListByJMJob(module);
        model.addAttribute("moduleList",moduleList);

        //获取所有父模块已经对应的子模块
        HashMap<Module,List<Module>> moduleHashMap=new HashMap<>();
        Module module2=new Module();
        for (Module module1 : moduleList) {
            module2.setModule_pid(module1.getModule_id());
            List<Module> moduleListByJMJob = iModuleManageService.getModuleListByJMJob(module2);
            moduleHashMap.put(module1,moduleListByJMJob);
        }
        model.addAttribute("moduleLists",moduleHashMap);

        return "sys/position/positionGrant1";
    }


    /**
     *进入模块详情页
     * @param module 存放模块信息
     * @return 执行条数
     */
    public String moduleView(Module module,Model model){
        List<Module> moduleListByJMJob = iModuleManageService.getModuleListByJMJob(module);
        model.addAttribute("moduleList",moduleListByJMJob.get(0));
        return "sys/modules/moduleView";
    }


    /**
     *增加某一条权限记录
     * @param permission 存放权限信息
     * @return 执行条数
     */
    public Integer addPermission(Permission permission) {
        return ijmMap.addPermission(permission);
    }



    /**
     *修改某一条权限记录状态
     * @param permission 存放权限信息
     * @return 执行条数
     */
    public Integer updatePermissionState(Permission permission) {
        return ijmMap.updatePermissionState(permission);
    }

    /**
     * 职位赋权(就是给权限表插入多条该职位的模块权限记录）
     * @param moduleGrant 该职位具有的权限模块
     * @return 执行条数
     */
    public String addPermission(String [] moduleGrant, HttpSession session, Model model){

        //先获取该职位的所有权限模块
        String comrid = (String) session.getAttribute("comrid");
        Permission permission=new Permission();
        permission.setComrid(comrid);
        List<Permission> permissionList = ijmMap.getPermissionListByPM(permission);
        String[] name =new String[100];
        if(permissionList!=null &&permissionList.size()>0){
            for (int i = 0; i < permissionList.size(); i++) {
                name[i]=permissionList.get(i).getModuleRname();
            }
        }
        boolean flag1=true;
        for (String s : name) {
            if(s!=null){
                flag1=false;
                break;
            }
        }


        //遍历选中的模块权限，与该职位具有的权限模块相比较，如果该职位已经具有该模块权限则修改该模块权限的状态为0，没有则新增一条模块权限记录
        for (String s : moduleGrant) {
            Permission permission1=new Permission();
            permission1.setComrid(comrid);
            Module module=new Module();
            module.setModule_rname(s);
            List<Module> moduleListByJMJob = iModuleManageService.getModuleListByJMJob(module);
            permission1.setCompid(moduleListByJMJob.get(0).getModule_id());
            if(flag1){  //name没有模块
                ijmMap.addPermission(permission1);
            }else {
                boolean flag=true;
                for (String s1 : name) {
                    if(s.equals(s1)){
                        flag=false;
                        break;
                    }
                }

                if(flag){
                    ijmMap.addPermission(permission1);
                }else {
                    permission1.setComstate("0");
                    updatePermissionState(permission1);
                }
            }


        }


        //遍历该职位具有的权限模块，与选中的模块权限相比，如果选中的里面有则跳过，没有则修改该模块权限的状态为1
        if(!flag1){  //要先name有模块
            for (String s : name) {
                if(s==null){
                    break;
                }else {
                    boolean flag=true;
                    for (String s1 : moduleGrant) {
                        if(s.equals(s1)){
                            flag=false;
                        }
                    }
                    if(flag){
                        Permission permission2=new Permission();
                        permission2.setComrid(comrid);
                        Module module=new Module();
                        module.setModule_rname(s);
                        List<Module> moduleListByJMJob = iModuleManageService.getModuleListByJMJob(module);
                        permission2.setCompid(moduleListByJMJob.get(0).getModule_id());
                        permission2.setComstate("1");
                        updatePermissionState(permission2);
                    }
                }
            }
        }


        return "true";
    }





}
